package com.px.lovepath.datebase;

import com.px.lovepath.LoveAppListener;
import com.px.lovepath.utils.LLog;

import android.content.Context;

public class DatabaseHelper {
	
	private static final String TAG = DatabaseHelper.class.getName();
	private static final int DB_HISTORY_VERSION_1 = 1;
	private static final int DB_FIRST_VERSION = DB_HISTORY_VERSION_1;
	private static final int DB_LATEST_VERSION = DB_HISTORY_VERSION_1;
	
	public static synchronized void upgrade(Context context, ManagedDatabase db) {
		// 初始化数据库
		int oldVersion = db.getVersion();
		try {
			db.beginTransaction();
			if (oldVersion < DB_FIRST_VERSION) {
				// 创建数据库
				db.setVersion(DB_LATEST_VERSION);
				createTables(db);
			} else {
				// 升级数据库
				
			}
			
			db.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
			return;
		} finally {
			db.endTransaction();
		}
	}
	
	// #### 实现函数 ####
	private static void createTables(ManagedDatabase db) {
		String createTableMessagesSql = String.format(
				"CREATE TABLE IF NOT EXISTS %s(%s INTEGER PRIMARY KEY, "
				+ " %s TEXT, "
				+ " %s TEXT, "
				+ " %s LONG, "
				+ " %s TEXT"
				+ ");"
				, 
					LoveColumns.TABLE_MESSAGE,
					LoveColumns._ID,
					LoveColumns.MESSAGE_PATH,
					LoveColumns.MESSAGE_INFO,
					LoveColumns.MESSAGE_TIME_STAMP,
					LoveColumns.MESSAGE_TYPE
				);
		LLog.f(TAG, "messages", createTableMessagesSql);
		db.execSQL(createTableMessagesSql);
	}
}
